<?php
class Profile extends CI_Controller{
	private $data;
	public function __construct(){
		parent::__construct();
		if(!Auth::isLoggedIn()) redirect("/");
		$this->load->model("model_user");
	}
	
	public function index(){
		$data['title'] = TITLE.' - Profile';
		if($this->input->post("btnSave")){
			$hasil = $this->_saveDetail();
			if($hasil) $data['success'] = "Data Sudah Dirubah";
		}
		$detail = $this->model_user->getDetail($this->session->userdata("user_id"));
		$data['detail'] = $detail;
		$this->load->view("profile",$data);
	}
	
	public function change_password(){
		$data['title'] = TITLE.' - Change Password';
		if($this->input->post("btnSave") ){
			$hasil = $this->_changepassword();
			if($hasil) $data['success'] = "Kata Sandi Sudah di rubah";
		}
		$this->load->view("changepassword",$data);
	}
	
	public function previous_order(){
		if($this->session->userdata("type_user") != "user") redirect("/");
		$data['title'] = TITLE.' - Previous Order';
		$this->load->view("previous_order",$data);
	}
	
	function detail_order($transaction_id=""){
		if($this->session->userdata("type_user") != "user") redirect("/");
		$data['title'] = TITLE.' - Detail Order';
		$this->load->view("order_detail",$data);
	}
	
	function _saveDetail(){
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               array(
                     'field'   => 'name',
                     'label'   => 'Name',
                     'rules'   => 'trim|required|xss_clean'
                  ),   
               array(
                     'field'   => 'email',
                     'label'   => 'Email',
                     'rules'   => 'trim|required|valid_email|callback__checkemail|xss_clean'
                  ),
			   array(
                     'field'   => 'phone',
                     'label'   => 'Phone',
                     'rules'   => 'trim|required|is_natural|xss_clean'
                  ),
			   array(
                     'field'   => 'address',
                     'label'   => 'Address',
                     'rules'   => 'trim|required|xss_clean'
                  )
            );

		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			//save to database..
			$this->model_user->edit_profile();
			return true;
		}
	}
	
	function _changepassword(){
		$this->form_validation->set_message('_checkpassword', 'Password lama salah');
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               array(
                     'field'   => 'password',
                     'label'   => 'Old Password',
                     'rules'   => 'trim|required|callback__checkpassword|xss_clean'
                  ),
               array(
                     'field'   => 'new_password',
                     'label'   => 'New Password',
                     'rules'   => 'trim|required|xss_clean'
                  ),
               array(
                     'field'   => 'new_password_c',
                     'label'   => 'Retype Password',
                     'rules'   => 'trim|required|matches[new_password]|xss_clean'
                  )
            );

		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			//save to database..
			$this->model_user->changepassword($this->input->post("new_password",true));
			return true;
		}
	}
	function _checkpassword($str){
		return $this->model_user->checkpassword($str);
	}
	function _checkemail($str){
		//cek dulu ada perubahan di email atau tidak.		
		$this->form_validation->set_message('_checkemail', 'Email already exists');
		
		$this->load->model("model_user");
		$detail = $this->model_user->getDetail($this->session->userdata("user_id"));
		if($str == $detail['email']) return true;
		
		$unique = $this->model_user->checkunique("email",$str);
		return $unique;
	}
}